using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Dyn.News.Extensions;
using Dyn.News.Routing;
using Dyn.News.Services;
using Orchard.Core.Feeds;
using Orchard.Core.Routable.Services;
using Orchard.DisplayManagement;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Services;
using Orchard.Themes;
using Orchard.UI.Navigation;
using Orchard.Settings;
using Orchard;
using Dyn.News.Models;

namespace Dyn.News.Controllers {

    [Themed]
    public class CategoryController : Controller {
        private readonly IOrchardServices _services;
        private readonly ICategoryervice _Categoryervice;
        private readonly IArticleService _ArticleService;
        private readonly ICategoryPathConstraint _categoryPathConstraint;
        private readonly IFeedManager _feedManager;
        private readonly IWorkContextAccessor _workContextAccessor;
        private readonly IHomePageProvider _routableHomePageProvider;
        private readonly ISiteService _siteService;

        public CategoryController(
            IOrchardServices services, 
            ICategoryervice Categoryervice,
            IArticleService ArticleService,
            ICategoryPathConstraint categoryPathConstraint,
            IFeedManager feedManager, 
            IShapeFactory shapeFactory,
            IWorkContextAccessor workContextAccessor,
            IEnumerable<IHomePageProvider> homePageProviders,
            ISiteService siteService) {
            _services = services;
            _Categoryervice = Categoryervice;
            _ArticleService = ArticleService;
            _categoryPathConstraint = categoryPathConstraint;
            _feedManager = feedManager;
            _workContextAccessor = workContextAccessor;
            _siteService = siteService;
            _routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
            Logger = NullLogger.Instance;
            Shape = shapeFactory;
        }

        dynamic Shape { get; set; }
        protected ILogger Logger { get; set; }

        public ActionResult List() {
            var Category = _Categoryervice.Get().Select(b => _services.ContentManager.BuildDisplay(b, "Summary"));

            var list = Shape.List();
            list.AddRange(Category);

            dynamic viewModel = Shape.ViewModel()
                .ContentItems(list);

            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return View((object)viewModel);
        }

        public ActionResult Item(string categoryPath, PagerParameters pagerParameters) {
            Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
            var correctedPath = _categoryPathConstraint.FindPath(categoryPath);
            if (correctedPath == null)
                return HttpNotFound();

            var CategoryPart = _Categoryervice.Get(correctedPath);
            if (CategoryPart == null)
                return HttpNotFound();

            // primary action run for a home paged item shall not pass
            if (!RouteData.DataTokens.ContainsKey("ParentActionViewContext")
                && CategoryPart.Id == _routableHomePageProvider.GetHomePageId(_workContextAccessor.GetContext().CurrentSite.HomePage)) {
                return HttpNotFound();
            }
            _feedManager.Register(CategoryPart);

            //var list = Shape.List();
            //list.AddRange(_ArticleService.Get(CategoryPart, pager.GetStartIndex(), pager.PageSize)
            //    .Select(b => Shape.Parts_Category_Article_ViewList(ContentItems: b)));
            IEnumerable<ArticlePart> list = _ArticleService.Get(CategoryPart, pager.GetStartIndex(), pager.PageSize);

            var totalItemCount = _ArticleService.PostCount(CategoryPart);
            Shape.Pager(pager).TotalItemCount(totalItemCount);
            var pagerShape = Shape.Pager(pager).TotalItemCount(totalItemCount);

            dynamic viewModel = Shape.ViewModel()
           .Articles(list)
           .Category(CategoryPart)
           .Pager(pagerShape);
            return View((object)viewModel);

            //var Articles = _ArticleService.Get(CategoryPart, pager.GetStartIndex(), pager.PageSize)



            ////    .Select(b => _services.ContentManager.BuildDisplay(b, "Summary"));
            //var Articles = _ArticleService.Get(CategoryPart, pager.GetStartIndex(), pager.PageSize)
            //    .Select(b => Shape.Parts_Category_Article_ViewList(ContentItems: b));


            //dynamic Category = Shape.Parts_Category_Category_Title(ContentItems: CategoryPart);

            //var list = Shape.List();
            //list.AddRange(Articles);
            //Category.Content.Add(Shape.Parts_Category_Article_List(ContentItems: list), "5");

            //var totalItemCount = _ArticleService.PostCount(CategoryPart);
            //Category.Content.Add(Shape.Pager(pager).TotalItemCount(totalItemCount), "Content:after");

            //return new ShapeResult(this, Category);
        }
    }
}